﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Globalization;
using System.IO;

public partial class Search : System.Web.UI.Page
{
    static MySql.Data.MySqlClient.MySqlConnection connection = new MySqlConnection(GetConnectionString());

    protected void Page_Load(object sender, EventArgs e)
    {

        MySql.Data.MySqlClient.MySqlConnection mycon = new MySqlConnection(GetConnectionString());

        try
        {
            mycon.Open();

        }
        catch (MySqlException ex)
        {

            throw (ex);

        }
    }
    public static string GetConnectionString()
    {
        string connStr = String.Format("server={0};user id={1}; password={2};" +
          "database=cs490; pooling=false", "bai.bounceme.net",
          "root", "purdue");

        return connStr;
    }
    public static void WriteError(string errorMessage)
    {
        try
        {
            string path = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
            }
            using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                w.WriteLine("\r\nLog Entry : ");
                w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                              ". Error Message:" + errorMessage;
                w.WriteLine(err);
                w.WriteLine("__________________________");
                w.Flush();
                w.Close();
            }
        }
        catch (Exception ex)
        {
            WriteError(ex.Message);
        }

    }
    static public LinkedList<string[]> searchDestionations(String terms)
    {
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader reader = null;
        LinkedList<String[]> returnlist = new LinkedList<String[]>();
        //WriteError(terms);
        string[] termslist = terms.Split(' ');
        
        command.CommandText = "select * from destination where Description REGEXP \"" + String.Join("|", termslist) + "\" OR Country REGEXP \"" + String.Join("|", termslist) + "\"";
        //Console.WriteLine(string.Join("|", termslist));
        //WriteError(command.CommandText);
        connection.Open();
        reader = command.ExecuteReader();
        while (reader.Read())
        {
            string[] addarr = new string[4];
            addarr[0] = reader.GetString(0);
            addarr[1] = reader.GetString(1);
            addarr[2] = reader.GetString(3);
            addarr[3] = reader.GetString(4);
            returnlist.AddFirst(addarr);
        }
        connection.Close();

        return returnlist;
    }
}